<?php

namespace app\admin\library;

use EasyWeChat\Factory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

/**
 *
 */
class Export
{


    public function __construct()
    {

    }

    public function exportExcel($expTitle,$expCellName,$packCellName,$goodsCellName,$max_goods_num,$expTableData){
        $fileName = $expTitle;
        $cellNum = count($expCellName);
        $dataNum = count($expTableData);

        $spreadsheet = new Spreadsheet();
        //$cellName = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ');
        $cellName = $this->getCellName();

        $sheet = $spreadsheet->getActiveSheet(0);

        $total_cell_num = count($expCellName) + count($goodsCellName) * $max_goods_num + count($packCellName) + 3;
        $sheet->getStyle('A1:' . $cellName[$total_cell_num] . '1')->getFont()->setBold(true);
        $sheet->getRowDimension('1')->setRowHeight(30);  //设置行高

        $i = 0;
        foreach ($expCellName as $key => $cell) {
            $sheet->setCellValue($cellName[$i] . '1', $cell);
            $i ++;
        }

        $color_start = $cellName[$i];
        foreach ($packCellName as $packCellName_k => $packCellName_v) {
            $sheet->setCellValue($cellName[$i] . '1', $packCellName_v);
            $i ++;
        }
        $color_end = $cellName[$i-1];
        $sheet->getStyle($color_start.'1:'.$color_end.'1')->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('EEFFBB');


        $color_start = $cellName[$i];
        for($n = 1;$n<=$max_goods_num;$n++){
            $k_n = 0;
            foreach ($goodsCellName as $goodsCellName_k => $goodsCellName_v) {
                if($goodsCellName_k == "goods_name" || $goodsCellName_k == "goods_name2"){
                    $goodsCellName_v = $goodsCellName_v.$n;
                }
                $sheet->setCellValue($cellName[$i] . '1', $goodsCellName_v);
                $i ++;
                $k_n ++;
            }
        }
        $color_end = $cellName[$i-1];
        $sheet->getStyle($color_start.'1:'.$color_end.'1')->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('FFFF00');

        for($i=0;$i<$dataNum;$i++){
            $j = 0;
            $data = $expTableData[$i];
            foreach ($expCellName as $expCellName_k => $expCellName_k_v) {
                $sheet->getCell($cellName[$j] . ($i + 2))->setValueExplicit($data["package"][$expCellName_k],'s');
                $j ++;
            }

            $color_start = $cellName[$j];
            foreach ($packCellName as $packCellName_k => $packCellName_v) {
                $sheet->getCell($cellName[$j] . ($i + 2))->setValueExplicit($data["package_end"][$packCellName_k],'s');
                $j ++;
            }
            $color_end = $cellName[$j-1];
            $sheet->getStyle($color_start.($i+2).':'.$color_end.($i+2))->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('EEFFBB');


            $color_start = $cellName[$j];
            foreach ($data["goods"] as $goods){
                foreach ($goodsCellName as $goodsCellName_k=>$goodsCellName_v){
                    $sheet->getCell($cellName[$j] . ($i + 2))->setValueExplicit($goods[$goodsCellName_k],'s');
                    $j++;
                }
            }
            $color_end = $cellName[$j-1];
            $sheet->getStyle($color_start.($i+2).':'.$color_end.($i+2))->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('FFFF00');
        }




        header('pragma:public');
        header('Content-type:application/vnd.ms-excel;charset=utf-8;name="' . $fileName . '.xlsx"');
        header("Content-Disposition:attachment;filename=$fileName.xlsx"); //attachment新窗口打印inline本窗口打印
        $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
        $writer->save('php://output');
    }

    public function exportExcel1($expTitle, $expCellName, $expTableData){
        $fileName = $expTitle;
        $cellNum = count($expCellName);
        $dataNum = count($expTableData);

        $spreadsheet = new Spreadsheet();
        //$cellName = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ');
        $cellName = $this->getCellName();
        $sheet = $spreadsheet->getActiveSheet(0);

        // for ($i = 0; $i < $cellNum; $i++) {
        //     $sheet->getColumnDimension($cellName[$i])->setWidth(30);
        // }

        $sheet->getStyle('A1:' . $cellName[$cellNum - 1] . '1')->getFont()->setBold(true);

        $i = 0;
        foreach ($expCellName as $key => $cell) {
            $sheet->setCellValue($cellName[$i] . '1', $cell);
            $i ++;
        }

        for ($i = 0; $i < $dataNum; $i++) {
            $j = 0;
            foreach ($expCellName as $key => $cell) {
                $sheet->getCell($cellName[$j] . ($i + 2))->setValueExplicit($expTableData[$i][$key],'s');
                $j++;
            }
        }

        header('pragma:public');
        header('Content-type:application/vnd.ms-excel;charset=utf-8;name="' . $fileName . '.xlsx"');
        header("Content-Disposition:attachment;filename=$fileName.xlsx"); //attachment新窗口打印inline本窗口打印
        $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
        $writer->save('php://output');
    }

    public function getCellName(){
        $cellName = ['','A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
        $cellName1 = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
        $data = [];
        foreach ($cellName as $v1){
            foreach ($cellName1 as $v2){
                $data[] =$v1?$v1.$v2:$v2;
            }
        }
        return $data;
    }
}
